#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit K1=P1^1;
sbit K2=P1^2;
sbit K3=P1^3;
sbit K4=P1^4;
sbit K5=P1^5;
sbit K6=P1^6;
sbit fmq=P3^6;
uchar hour=07,min=59,sec=55;
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay(uint i)
{
	uchar j;
	while(i--)
		for(j=0;j<125;j++);
}

void display()
{
	P2=0x01;
	P0=tab[hour/10];
	delay(1);
	P2=0x02;
	P0=tab[hour%10];
	delay(1);
	
	P2=0x04;
	P0=0xbf;
	delay(1);
	
	P2=0x08;
	P0=tab[min/10];
	delay(1);
	P2=0x10;
	P0=tab[min%10];
	delay(1);
	
	P2=0x20;
	P0=0xbf;
	
	delay(1);
	P2=0x40;
	P0=tab[sec/10];
	P2=0x80;
	P0=tab[sec%10];
}

void Keyscan()
{
	if(K1==0)
	{
		delay(150);
		if(K1==0)
		{
			hour=hour+1;
			if(hour==24)
				hour=0;
		}
	}
		if(K2==0)
		{
			delay(150);
			if(K2==0)
			{
				hour=hour-1;
				if(hour==-1)
					hour=23;
			}
		}
	if(K3==0)
	{
		delay(150);
		if(K3==0)
		{
			min=min+1;
			if(min==60)
				min=0;
		}
	}
	if(K4==0)
	{
		delay(150);
		if(K4==0)
		{
			min=min-1;
			if(min==-1)
				min=59;
		}
	}
	if(K5==0)
	{
		delay(150);
		if(K5==0)
		{
			sec=sec+1;
			if(sec==60)
				sec=0;
		}
	}
	if(K6==0)
	{
		delay(150);
		if(K6==0)
		{
			sec=sec-1;
			if(sec==-1)
				sec=59;
		}
	}
}
void main()
{
	EA=1;
	EX1=1;
	IT1=1;
	
	TMOD=0x01;
	TH0=(65536-1000)/256;
	TL0=(65536-1000)%256;
	TR0=1;
	ET0=1;
	
	TMOD=0x10;
	TH1=(65536-50000)/256;
	TL1=(65536-50000)%256;
	TR1=1;
	ET1=1;
	while(1)
	{
		display();
		Keyscan();
	}
}
void JS(void) interrupt 3
{
	uint t;
	t=t+1;
	if(t==20)
	{
		if(sec==60)
		{
			sec=0;
		min++;
		if(min==60)
		{
			min=0;
			hour++;
	
		if(hour==24)
		{
			hour=0;
		}
	}
}
t=0;
}
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
}
void int1(void) interrupt 2
{
	EX1=0;
	hour=7;min=22;sec=59;
	EX1=1;
}
void Time0(void) interrupt 1
{
	if(hour==8&&min==0&&sec==0)
	fmq=~fmq;
	TH0=(65536-1000)/256;
	TL0=(65536-1000)%256;
}
